package com.sanuo.pico;

import android.app.Activity;
import android.view.Gravity;

import com.menu.app.MyApp;
import com.menu.app.activity.base.MenuBaseActivity;
import com.menu.app.activity.base.PortBaseActivity;
import com.menu.app.it.Reset;
import com.menu.app.model.Models;
import com.menu.app.util.Constant;
import com.menu.app.util.FunctionCode;
import com.menu.app.util.Pdf;
import com.menu.app.util.pipe.BasePortEventPipe;
import com.menu.app.util.port.SerialHandler;
import com.menu.app.util.port.SerialInterface;
import com.plata.base.aclr.utils.SPHelper;
import com.sanuo.pico.act.BoardActivity;
import com.sanuo.pico.act.CustomInfoActivity;
import com.sanuo.pico.act.SettingMenuActivity;
import com.sanuo.pico.act.work.ModeActivity;
import com.sanuo.pico.adapter.MyBootAdapter;
import com.sanuo.pico.adapter.MyLanguageAdapter;
import com.sanuo.pico.bean.WorkParam;
import com.sanuo.pico.event.Event;
import com.sanuo.pico.it.WorkingPage;
import com.sanuo.pico.utils.MySerialHandle;
import com.sanuo.pico.utils.MySerialHandler;
import com.sanuo.pico.utils.PortPipe;
import com.sanuo.pico.view.MyPopMenu;

import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class App extends MyApp {

    static App sInstance;

    @Override
    public String getUmId() {
        return "";
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        Constant.PRODUCT = BuildConfig.PRODUCT;
        if (isDefault()) {
            FunctionCode.START1 = 0x5a;
            FunctionCode.START2 = (byte) 0xa5;
            FunctionCode.END1 = (byte) 0xa5;
            FunctionCode.END2 = (byte) 0xa5;
            Constant.degree = 0;

            Constant.MENU_GRAVITY = Gravity.BOTTOM;
            MenuBaseActivity.popMenuClass = MyPopMenu.class;
            Constant.LANGUAGE_ADAPTER = MyLanguageAdapter.class.getName();
            Constant.BOOT_ADAPTER = MyBootAdapter.class.getName();


//            EventBus.getDefault().register(this);
            boolean bo = SPHelper.getInstance(this).getBoolean("rent", false);
            Models.Companion.get().getRentEvent().setValue(bo ? 1 : 0);
            long i = SPHelper.getInstance(App.getApp()).getLong("rent_point", 0L);
            if (i == 0) {
                i = SPHelper.getInstance(App.getApp()).getInt("rent_point", 0);
            }
            Models.Companion.get().getPointEvent().postValue(i);
            int j = SPHelper.getInstance(App.getApp(), true).getInt("hConnect", 0);
            Event.connect.setValue(j);
        }
    }

    @Override
    public void back2Main(MenuBaseActivity.JumpPrepareListener listener) {
        ignore = true;
        Iterator<Activity> it = list.iterator();
        while (it.hasNext()) {
            Activity activity = it.next();
            if (activity instanceof PortBaseActivity && !isWorkingPage(activity)) {
                ((PortBaseActivity) activity).exit();
            }
        }
        if (listener != null) {
            listener.onPrepared();
        }
    }

    @Override
    public Reset getReset() {
        return null;
    }

    @Override
    protected void toBackground() {
    }

    @Override
    public boolean isWorkingPage() {
        Activity ac = MyApp.getApp().getTopActivity();
        return isWorkingPage(ac);
    }

    @Override
    public boolean isWorkingPage(Activity activity) {
        return activity instanceof WorkingPage;
    }

    public boolean isPwd() {
        return false;
    }

    @Override
    public Class getMainClass() {
        return ModeActivity.class;
    }

    public static App getApp() {
        return sInstance;
    }

    @Override
    public Map getPageMap() {
        return Collections.emptyMap();
    }

    @Override
    public int getRate() {
        return 19200;
    }

    @Override
    public Class getBoard() {
        return BoardActivity.class;
    }

    @Override
    public BasePortEventPipe getPortPipe() {
        return PortPipe.getInstance();
    }

    @Override
    public Class getSettingClass() {
        return SettingMenuActivity.class;
    }

    @Override
    public Class getCustomInfoClass() {
        return CustomInfoActivity.class;
    }

    @Override
    public Pdf getPdfCreator() {
        return null;
    }

    MySerialHandler serialHandler;

    @Override
    public SerialHandler getSerialHandler(int type) {
        if (type == 1) {
            return null;
        }
        if (serialHandler == null) {
            serialHandler = new MySerialHandler();
        }
        return serialHandler;
    }

    @Override
    public SerialInterface getSerialInterface(int type) {
        return new MySerialHandle(getSendType(), 10, TimeUnit.MILLISECONDS);
    }

    WorkParam param;

    public void setWorkParam(WorkParam p) {
        param = p;
    }

    public WorkParam getWorkParam() {
        return param;
    }
}
